﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using FairyGUI;
using Model;
namespace MDFGame
{
    /// <summary>
    /// 卡片组装工厂
    /// </summary>
    public static class CardFactory
    {
        /// <summary>
        /// 创建新卡牌实体【创建的单位无所属关系和区域位置】
        /// </summary>
        /// <param name="name">卡片名称[缩写[RM-C-1]]</param>
        /// <param GID="GID">卡片游戏中临时ID</param>
        /// <returns></returns>
        public static async Task<Entity> CreateNewCard(string name,int GID=-1)
        {
            GlobalComponent _gol = GameRoot.Scene.GetComponent<GlobalComponent>();
            if (_gol.SqlManager == null)
            {
                return null;
            }
            Card _card=EntityFactory.Create<Card>();
            //装载卡片信息组件
            CardInfoComponent _cic = _card.AddComponent<CardInfoComponent>();
            _cic.CardGID = GID;
            _cic.CardBasicInfo = _gol.SqlManager.GetCardByName(name);
            _cic.CType = _cic.CardBasicInfo.CType;
            _cic.Owner = Owner.Unknown;
            _cic.NowArea = AreaType.Unknown;
            _cic.ToArea = AreaType.Unknown;
            _cic.TempNum = new Dictionary<NumType, int>();
            foreach (NumType v in _cic.TempNum.Keys)
                _cic.TempNum[v] = -1;//初始化为-1

            //装载卡片UI组件
            AssetBundle _ab = await FileIOHelper.GetAssetBundle($"{_gol.AssetPath}/pic/{name}.ab");
            CardUIComponent _cuc = _card.AddComponent<CardUIComponent>();
            _cuc.cardState = CardStateType.DOWN | CardStateType.ERECTION;
            _cuc.CanDrag = false;
            _cuc.CanClick = false;
            _cuc.CanTouch = true;
            _cuc.CanTurn = false;
            _cuc.CardTexture = _ab.LoadAsset<Texture2D>($"{name}.jpg");


            //装载卡片效果组件
            CardEffectComponent _cec = _card.AddComponent<CardEffectComponent>();
            _cec.CanUseEffect = false;
            _cec.IsBuffActive = true;
            _cec.BuffPackage = new List<BuffPackage>();
            _cec.Effect = EffectFactory.GetCardEffect(name);

            switch (_cic.CType)
            {
                case CardType.Character:
                    //角色卡没有数值
                    //角色称号+角色名
                    CharacterInfo _c = (CharacterInfo)_cic.CardBasicInfo;
                    _cic.CardDescribe = $"{_c.Title}:{_c.CardName}/n";
                    _cuc.MainView = new CharacterCardUI();
                    _cuc.MainView.Create(UIPackage.CreateObject("cardUI","CharacterCard").asCom, _cuc);
                    //描述信息为角色效果
                    foreach (Skill s in _c.Skills)
                    {
                        _cic.CardDescribe += $"{s.Type.ToString()}:{s.SkillText}/n";
                    }
                    break;
                case CardType.Battle:
                    //战斗卡初始化代价战斗力华丽度
                    BattleInfo _b = (BattleInfo)_cic.CardBasicInfo;
                    _cic.TempNum[NumType.Level] = _b.Level;
                    _cic.TempNum[NumType.Battle] = _b.BattleValue;
                    _cic.TempNum[NumType.Cost] = _b.Cost;
                    _cuc.MainView = new BattleCardUI();
                    _cuc.MainView.Create(UIPackage.CreateObject("cardUI", "BattleCard").asCom, _cuc);
                    //描述信息为条件+效果描述+特性表
                    _cic.CardDescribe = $"{_b.CardName}-{_gol.SqlManager.GetSeries(_b.Series)}/n条件：{_b.CText}/n  {_b.EText}/n特性：";
                    foreach (int i in _b.TList)
                    {
                        _cic.CardDescribe += $"{_gol.SqlManager.GetTEffect(i).TName},";
                    }
                    break;
                case CardType.Special:
                    //特殊卡初始化代价华丽度
                    SpecialInfo _s = (SpecialInfo)_cic.CardBasicInfo;
                    _cic.TempNum[NumType.Level] = _s.Level;
                    _cic.TempNum[NumType.Cost] = _s.Cost;
                    _cuc.MainView = new SpecialCardUI();
                    _cuc.MainView.Create(UIPackage.CreateObject("cardUI", "SpecialCard").asCom, _cuc);
                    //描述信息为条件+效果描述
                    _cic.CardDescribe = $"{_s.CardName}-{_gol.SqlManager.GetSeries(_s.Series)}/n条件：{_s.CText}/n  {_s.EText}/n";
                    break;
                default:
                    break;
            }

            return _card;
        }
    }
}
